package cn.wolfcode.p2p.business.controller;

import cn.wolfcode.p2p.anno.NeedLogin;
import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.query.PaymentScheduleQueryObject;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IRealAuthSevice;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.ExpAccount;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.IExpAccountService;
import cn.wolfcode.p2p.business.service.IPaymentScheduleService;
import cn.wolfcode.p2p.util.Constant;
import cn.wolfcode.p2p.util.JSONResultUtil;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 前台我要借款认证页面
 */
@Controller
public class BorrowController {
    @Autowired
    private IAccountService accountService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IBidRequestService bidRequestService;

    @Autowired
    private IRealAuthSevice realAuthSevice;

    @Autowired
    private IPaymentScheduleService paymentScheduleService;

    @Autowired
    private IExpAccountService expAccountService;


    @RequestMapping("borrow")
    public String borrow(Model model) {
        if (UserContext.getLoginInfo() == null) {
            return "redirect:/borrow.html";
        }

        //获取account对象
        model.addAttribute("account", accountService.getAccount(UserContext.getLoginInfo().getId()));
        //获取userInfo对象
        model.addAttribute("userinfo", userInfoService.getUserInfo(UserContext.getLoginInfo().getId()));
        return "borrow";
    }

    /**
     * 借款申请页面
     */
    @NeedLogin
    @RequestMapping("borrow_page")
    public String borrowPage(Model model) {
        //判断用户是否有处在借款流程的位状态
        UserInfo userInfo = userInfoService.getUserInfo(UserContext.getLoginInfo().getId());
        if(userInfo.hasLoan()){
            return "borrow_apply_result";
        }
        //最小借款金额
        model.addAttribute("minBidRequestAmount", Constant.SMALLEST_BIDREQUEST_AMOUNT);
        //最大借款金额
        model.addAttribute("account", accountService.getAccount(UserContext.getLoginInfo().getId()));
        //最小投标金额
        model.addAttribute("minBidAmount", Constant.SMALLEST_BID_AMOUNT);
        return "borrow_apply";
    }

    /**
     * 申请借款
     */
    @NeedLogin
    @RequestMapping("borrow_apply")
    @ResponseBody
    public Object apply(BidRequest bidRequest) {
        bidRequestService.apply(bidRequest);
        return JSONResultUtil.jsonResult();
    }

    /**
     * 借款详情
     */
    @RequestMapping("borrow_info")
    public String borrowInfo(Long id,Integer bidRequestType,Model model){
        //借款
        BidRequest bidRequest = bidRequestService.getBidRequestById(id);
        model.addAttribute("bidRequest",bidRequest);
        LoginInfo loginInfo = UserContext.getLoginInfo();
        if(bidRequestType == Constant.BIDREQUEST_TYPE_NORMAL){
            //借款人信息
            UserInfo userInfo = userInfoService.getUserInfo(bidRequest.getCreateUser().getId());
            model.addAttribute("userInfo",userInfo);
            model.addAttribute("realAuth",realAuthSevice.getRealAuth(userInfo.getRealAuthId()));
            //投资人信息
            //如果当前登录对象不是借款人,则显示信息
            if(loginInfo != null){
                if(loginInfo.getId().longValue() == userInfo.getId().longValue()){
                    model.addAttribute("self",true);
                }else{
                    Account account = accountService.getAccount(loginInfo.getId());
                    model.addAttribute("account",account);
                }

            }
        } else if(bidRequestType == Constant.EXPERIENCE_BIDREQUEST_TYPE_CREDITTRANSFER){
            if(loginInfo != null){
                ExpAccount expAccount = expAccountService.getExpAccount(loginInfo.getId());
                model.addAttribute("account",expAccount);
            }
        }
        return "borrow_info";
    }

    /**
     * 还款计划页面
     */
    @NeedLogin
    @RequestMapping("borrowBidReturn_list")
    public String borrowBidReturnList(@ModelAttribute("qo")PaymentScheduleQueryObject qo, Model model){
        Long userId = UserContext.getLoginInfo().getId();
        qo.setUserId(userId);
        model.addAttribute("account",accountService.getAccount(userId));
        model.addAttribute("pageResult",paymentScheduleService.queryForPage(qo));
        return "returnmoney_list";
    }

    /**
     * 还款
     */
    @NeedLogin
    @RequestMapping("returnMoney")
    @ResponseBody
    public Object returnMoney(Long id){
        bidRequestService.returnMoney(id);
        return JSONResultUtil.jsonResult();
    }
}
